#!/bin/bash

# ComfyUI SDK Rust - Local Image Upload Examples
# This script demonstrates how to run the local image upload examples

echo "🚀 ComfyUI SDK Rust - Local Image Upload Examples"
echo "=================================================="

# Check if image file exists
IMAGE_FILE="20250808-111737.png"
if [ ! -f "$IMAGE_FILE" ]; then
    echo "❌ Image file not found: $IMAGE_FILE"
    echo ""
    echo "💡 Please ensure you have an image file named '$IMAGE_FILE' in the current directory"
    echo "   You can:"
    echo "   1. Copy your image file and rename it to '$IMAGE_FILE'"
    echo "   2. Or modify the image path in the example code"
    echo ""
    echo "   Supported formats: PNG, JPG, JPEG, BMP, TIFF, WEBP"
    exit 1
fi

echo "✅ Found image file: $IMAGE_FILE"
echo ""

# Function to run example with error handling
run_example() {
    local example_name=$1
    local bin_name=$2
    
    echo "🔄 Running $example_name..."
    echo "Command: cargo run --bin $bin_name"
    echo "----------------------------------------"
    
    if cargo run --bin "$bin_name"; then
        echo "✅ $example_name completed successfully!"
    else
        echo "❌ $example_name failed!"
        echo ""
        echo "💡 Troubleshooting tips:"
        echo "   1. Make sure ComfyUI server is running at http://192.168.0.193:8188"
        echo "   2. Check network connectivity to the server"
        echo "   3. Verify the image file is valid and readable"
        echo "   4. Ensure ComfyUI has all required nodes installed"
    fi
    
    echo ""
    echo "=========================================="
    echo ""
}

# Show menu
echo "📋 Available Examples:"
echo "   1. Simple Local Image Upload (recommended for first-time users)"
echo "   2. Real Local Image Test (comprehensive with error handling)"
echo "   3. Run both examples"
echo ""

read -p "Choose an option (1-3): " choice

case $choice in
    1)
        echo "🎯 Running Simple Local Image Upload Example"
        echo ""
        run_example "Simple Local Image Upload" "simple-local-image"
        ;;
    2)
        echo "🎯 Running Real Local Image Test Example"
        echo ""
        run_example "Real Local Image Test" "real-local-image-test"
        ;;
    3)
        echo "🎯 Running Both Examples"
        echo ""
        run_example "Simple Local Image Upload" "simple-local-image"
        run_example "Real Local Image Test" "real-local-image-test"
        ;;
    *)
        echo "❌ Invalid choice. Please run the script again and choose 1, 2, or 3."
        exit 1
        ;;
esac

echo "🎉 Example execution completed!"
echo ""
echo "📚 For more information:"
echo "   - Read REAL_LOCAL_IMAGE_EXAMPLES.md for detailed documentation"
echo "   - Check README-RUST.md for general SDK usage"
echo "   - View source code in src-rs/examples/ for implementation details"
